﻿package org.nickro.project.widgets{
    import flash.events.*;

    public class ToggleButton extends PushButton {

        private var pIsOn:Boolean;
        private var pOnLabel:String;// = ""
        public var pToggleWhenClicked:Boolean;
        private var pOffLabel:String;// = ""

        private static const FRAME_ON_DOWN:int = 7;
        private static const FRAME_OFFSET:int = 3;
        private static const FRAME_ON_UP:int = 5;
        private static const FRAME_ON_OVER:int = 6;

        public function ToggleButton(){
            super();
            pIsOn = false;
            pToggleWhenClicked = true;
        }
        override public function setLabel(labelHtml:String, auto_size:String="none"):void{
            pOffLabel = labelHtml;
            pOnLabel = labelHtml;
            super.setLabel(labelHtml, auto_size);
        }
        override public function clickHandler():void{
            if (pEnabled){
                if (pToggleWhenClicked){
                    setIsOn(!(pIsOn));
                };
                dispatchEvent(new Event(pClickEventType, true));
                if (((pUsingSounds) && (pClickChan))){
                    pClickChan.play();
                };
            };
        }
        override public function enable(onFlag:Boolean, showFlag:Boolean=true):void{
            super.enable(onFlag, showFlag);
            if (((((showFlag) && (pIsOn))) && (pEnabled))){
                gotoAndStop(FRAME_ON_UP);
            };
        }
        public function setLabels(offLabelHtml:String, onLabelHtml:String, auto_size:String="none"):void{
            pOffLabel = offLabelHtml;
            pOnLabel = onLabelHtml;
            super.setLabel((pIsOn) ? onLabelHtml : offLabelHtml, auto_size);
        }
        public function initialiseToggle(offLabel:String, onLabel:String, btnID:String, eventListener:Function=null, eventType:String="clickPushButton"):void{
            super.initialise(null, btnID, eventListener, eventType);
            if ((((offLabel is String)) && ((onLabel is String)))){
                setLabels(offLabel, onLabel);
            };
        }
        override protected function rollOverListener(e:MouseEvent):void{
            if (pEnabled){
                pMouseOver = true;
                gotoAndStop((pIsOn) ? FRAME_ON_OVER : FRAME_OVER);
                if (((pUsingSounds) && (pRollChan))){
                    pRollChan.play();
                };
            };
        }
        public function isOn():Boolean{
            return (pIsOn);
        }
        override protected function rollOutListener(e:MouseEvent):void{
            if (pEnabled){
                pMouseOver = false;
                gotoAndStop((pIsOn) ? FRAME_ON_UP : FRAME_UP);
            };
        }
        override protected function mouseUpListener(e:MouseEvent):void{
            if (pEnabled){
                if (pMouseOver){
                    gotoAndStop((pIsOn) ? FRAME_ON_OVER : FRAME_OVER);
                } else {
                    gotoAndStop((pIsOn) ? FRAME_ON_UP : FRAME_UP);
                };
                if (pTrackAsMenu){
                    clickHandler();
                };
            };
        }
        public function setIsOn(isOn:Boolean):void{
            var frameOffset:int;
            if (isOn != pIsOn){
                pIsOn = isOn;
                if (pUsingLabel){
                    iLabel_txt.htmlText = (pIsOn) ? pOnLabel : pOffLabel;
                };
                frameOffset = (pIsOn) ? FRAME_OFFSET : -(FRAME_OFFSET);
                gotoAndStop((currentFrame + frameOffset));
            };
        }
        override protected function mouseDownListener(e:MouseEvent):void{
            if (pEnabled){
                gotoAndStop((pIsOn) ? FRAME_ON_DOWN : FRAME_DOWN);
            };
        }
        public function getLabels(asHTML:Boolean=false):Array{
            var oldLabel:String = iLabel_txt.htmlText;
            iLabel_txt.htmlText = pOffLabel;
            var offLabel:String = (asHTML) ? iLabel_txt.htmlText : iLabel_txt.text;
            iLabel_txt.htmlText = pOnLabel;
            var onLabel:String = (asHTML) ? iLabel_txt.htmlText : iLabel_txt.text;
            iLabel_txt.htmlText = oldLabel;
            return ([offLabel, onLabel]);
        }
        public function toggleIsOn():Boolean{
            setIsOn(!(pIsOn));
            return (pIsOn);
        }
        override protected function clickListener(e:MouseEvent):void{
            if (pEnabled){
                if (!pTrackAsMenu){
                    clickHandler();
                };
            };
        }

    }
}
